const pg = require('pg');
const logger = require('./logger')
const PostgreSQL = {
  connect: (connectInfo) => {
    return new Promise(function (resolve, reject) {
      const pool = new pg.Pool(connectInfo);
      pool.connect(function (error, client, done) {
        if (error) {
          logger.info(`PostgreSQL connect`, error)
          reject(error)
        }
        logger.info(`PostgreSQL connect`, client)
        resolve(client);
      })
    });
  },
  close: conn => {
    return new Promise(function (resolve, reject) {
      conn.end(err => {
        if (err) {
          reject(err)
          logger.info(`PostgreSQL close`, err)
        }
        resolve();
      });
    })
  }
}

module.exports = {
  PostgreSQL
};